package com.cloudsigns.factorymanager2.engineering.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.cloudsigns.factorymanager2.R;
import com.cloudsigns.factorymanager2.cusenum.CommonEnum;
import com.cloudsigns.factorymanager2.engineering.IView.IReceiveEngineeringOrderListView;
import com.cloudsigns.factorymanager2.engineering.bean.OrderParentBean;
import com.cloudsigns.factorymanager2.engineering.presenter.ReceiveEngineeringOrderListPresent;
import com.cloudsigns.factorymanager2.project.activity.SurveyCompletionOrderForProjectActivity;
import com.cloudsigns.factorymanager2.project.adapter.EngineeringOrderListAdapter;
import com.cloudsigns.factorymanager2.project.customview.DropDownPoupWindow;
import com.cloudsigns.factorymanager2.project.utils.ConstantUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by tony on 2018/5/19.
 */

public class SuccessOrderListForMnagerActivity extends Activity implements View.OnClickListener, IReceiveEngineeringOrderListView {
    private TextView tv_title;
    private ListView lv;
    private View rl_progressbar;
    private ViewGroup rl_back;
    private RelativeLayout rl_right;
    private ImageView img_tri_right;
    private EngineeringOrderListAdapter adapter;
    private List<OrderParentBean> orderParentBeanList = new ArrayList<>();
    private ReceiveEngineeringOrderListPresent receiveEngineeringOrderListPresent;
    private final String TAG = SuccessOrderListForMnagerActivity.class.getSimpleName();

    private DropDownPoupWindow popupWindow;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏
        // 沉浸式状态栏
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
        setContentView(R.layout.layout_create_engineering_order_list);

        receiveEngineeringOrderListPresent = new ReceiveEngineeringOrderListPresent(this);
        init();
        getOrderList();
    }

    private void init() {
        rl_back = findViewById(R.id.rl_back);
        rl_right=findViewById(R.id.rl_right);
        tv_title = findViewById(R.id.tv_title);
        lv = findViewById(R.id.lv);
        rl_progressbar = findViewById(R.id.rl_progressbar);
        img_tri_right=findViewById(R.id.img_tri_right);

        tv_title.setText("完工资料");
        rl_back.setOnClickListener(this);
        rl_right.setOnClickListener(this);

        adapter = new EngineeringOrderListAdapter(this, TAG);
        adapter.setData(orderParentBeanList);
        lv.setAdapter(adapter);

        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                Intent mIntent;
                OrderParentBean order = orderParentBeanList.get(position);
                // 工程安装
                if (order.getCategory() == CommonEnum.EngineeringORderCategory.INSTALL) {
                    //mIntent = new Intent(SuccessOrderListForMnagerActivity.this, EngineeringInstallCompletionOrderForManagerActivity.class);
                    mIntent = new Intent(SuccessOrderListForMnagerActivity.this, EngineeringInstallCompletionOrderForManagerActivity1.class);
                    mIntent.putExtra(ConstantUtil.KEY_1, order);
                    startActivityForResult(mIntent, ConstantUtil.START_ACTIVITY_KEY_1);
                }
                // 工程施工
                else if (order.getCategory() == CommonEnum.EngineeringORderCategory.CONSTRUCT) {
                    // mIntent = new Intent(SuccessOrderListForMnagerActivity.this, SendOrderForSuperActivity.class);
                    mIntent = new Intent(SuccessOrderListForMnagerActivity.this, EngineeringConstructCompletionOrderForManagerActivity.class);
                    mIntent.putExtra(ConstantUtil.KEY_1, order);
                    startActivityForResult(mIntent, ConstantUtil.START_ACTIVITY_KEY_1);
                }
                // 批量安装
                else if (order.getCategory() == CommonEnum.EngineeringORderCategory.BATCH_INSTALL) {
                   // mIntent = new Intent(SuccessOrderListForMnagerActivity.this, DispatchEngineeringBatchInstallationOrderActivity.class);
                    mIntent = new Intent(SuccessOrderListForMnagerActivity.this, EngineeringBatchInstallationCompletionOrderForManagerActivity.class);
                    mIntent.putExtra(ConstantUtil.KEY_1, order);
                    startActivity(mIntent);
//                    mIntent = new Intent(CreateEngineeringOrderListActivity.this, EditBatchInstallationEngineeringActivity.class);
//                    startActivity(mIntent);
                }
                // 维修单
                else if (order.getCategory() == CommonEnum.EngineeringORderCategory.MAINTAIN) {
                   // mIntent = new Intent(SuccessOrderListForMnagerActivity.this, DispatchEngineeringMaintenanceOrderActivity.class);
                    mIntent = new Intent(SuccessOrderListForMnagerActivity.this, EngineeringMaintenanceCompletionOrderForManagerActivity.class);
                    mIntent.putExtra(ConstantUtil.KEY_1, order);
                    startActivity(mIntent);
                }
                // 广告上刊
                else if (order.getCategory() == CommonEnum.EngineeringORderCategory.PUBLICATION) {
                   // mIntent = new Intent(SuccessOrderListForMnagerActivity.this, PublicationCompletionOrderForSuperActivity.class);
                    mIntent = new Intent(SuccessOrderListForMnagerActivity.this, EngineeringPublicationCompletionOrderForManagerActivity.class);
                    mIntent.putExtra(ConstantUtil.KEY_1, TAG);
                    mIntent.putExtra(ConstantUtil.KEY_2, order);
                    startActivityForResult(mIntent, ConstantUtil.START_ACTIVITY_KEY_1);
                }
                // 巡查
                else if (order.getCategory() == CommonEnum.EngineeringORderCategory.PATROL) {
                    mIntent = new Intent(SuccessOrderListForMnagerActivity.this, PatrolCompletionOrderForManagerActivity.class);
                    mIntent.putExtra(ConstantUtil.KEY_1, order);
                    startActivityForResult(mIntent, ConstantUtil.START_ACTIVITY_KEY_1);
                }
                // 现场勘察
                else if (order.getCategory() == CommonEnum.EngineeringORderCategory.SURVEY) {
                   // mIntent = new Intent(SuccessOrderListForMnagerActivity.this, SurveyCompletionOrderForProjectActivity.class);
                    mIntent = new Intent(SuccessOrderListForMnagerActivity.this, EngineeringSurveyCompletionOrderForManagerActivity.class);
                    mIntent.putExtra(ConstantUtil.KEY_1, order);
                    startActivityForResult(mIntent, ConstantUtil.START_ACTIVITY_KEY_1);
                }
            }
        });
    }

    private void getOrderList() {
        receiveEngineeringOrderListPresent.getReceiveEngineeringOrderList();
    }

    private void showPopupWindow(View anchor) {
        popupWindow = new DropDownPoupWindow(SuccessOrderListForMnagerActivity.this, TAG,-1, R.layout.popwindow_work_sheets, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        img_tri_right.setVisibility(View.VISIBLE);
        popupWindow.setOnClickListener(this);
        popupWindow.setCusOnDismissListener(new PopupWindow.OnDismissListener() {

            @Override
            public void onDismiss() {
                img_tri_right.setVisibility(View.GONE);
            }
        });
        popupWindow.showPopupWindow(anchor, anchor.getLayoutParams().width / 2, 5);
    }

    @Override
    public void onClick(View view) {
        Intent mIntent;

        switch (view.getId()) {
            case R.id.rl_back:
                finish();
                break;
            case R.id.rl_right:
                showPopupWindow(view);
                break;

        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (null != data) {
            switch (requestCode) {
                case ConstantUtil.START_ACTIVITY_KEY_1:
                    getOrderList();
                    break;
            }
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public void showLoading() {
        rl_progressbar.setVisibility(View.VISIBLE);
    }

    @Override
    public void hideLoading() {
        rl_progressbar.setVisibility(View.GONE);
    }

    @Override
    public void showFailedError(String desc) {
        Toast.makeText(this, desc, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showData(List<OrderParentBean> parentBeanList, List<OrderParentBean> engineeringOrderBeanList_status_send, List<OrderParentBean> engineeringOrderBeanList_status_receives, List<OrderParentBean> engineeringOrderBeanList_status_returns, List<OrderParentBean> engineeringOrderBeanList_status_success) {
        if (null != parentBeanList) {
            orderParentBeanList.clear();
            orderParentBeanList.addAll(parentBeanList);
            adapter.setData(orderParentBeanList);
            adapter.notifyDataSetChanged();
        }
    }
}
